package com.drug.mingkang.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.drug.mingkang.model.HqImm;
import com.drug.mingkang.model.ImmFinance;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqImmService;
import com.drug.mingkang.service.ImmFinanceService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (HqImm)表控制层
 *
 * @author wutao
 * @since 2020-08-11 12:00:13
 */
@RestController
@RequestMapping("hqImm")
public class HqImmController {
    /**
     * 服务对象
     */
    @Resource
    private HqImmService hqImmService;

    @Resource
    private ImmFinanceService immFinanceService;

    @Resource
    private HttpSession session;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public HqImm selectOne(String id) {
        return this.hqImmService.queryById(id);
    }

    @ResponseBody
    @RequestMapping("showList")
    public Map<String,Object> show(HqImm hqImm){
        List<HqImm> list = hqImmService.queryAllByLimit(hqImm);
        int count = hqImmService.count();
        Tools tools = new Tools();
        return tools.limit(list,count);
    }

    @ResponseBody
    @RequestMapping("deleteByID")
    public ResponseMessage deleteByID(String immId){
        ResponseMessage rm = new ResponseMessage();
        int rows = hqImmService.deleteById(immId);
        if(rows > 0){
            rm.setStatus(1);
            rm.setMessage("成功");
        }else {
            rm.setStatus(0);
            rm.setMessage("失败");
        }
        return rm;
    }

    @RequestMapping("/addORupdate")
    @ResponseBody
    public ResponseMessage addOrupdate(HqImm hqImm){
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        int rows = 0;
        ImmFinance immFinance = new ImmFinance();
        if(StringUtils.isEmpty(hqImm.getImmId())){
            hqImm.setOper(perUsers.getUserName());
            rows = hqImmService.insert(hqImm);
            immFinance.setImmId(hqImm.getImmId());
            immFinance.setIfnanceMoney(hqImm.getIfnanceMoney());
            immFinanceService.insert(immFinance);

        }else{
            hqImm.setOper(perUsers.getUserName());
            rows = hqImmService.update(hqImm);
        }
        ResponseMessage rm = new ResponseMessage();
        rm.setStatus(rows);
        return rm;
    }

    @RequestMapping("queryById")
    @ResponseBody
    public HqImm queryById(String id){
        HqImm hqImm = hqImmService.queryById(id);
        System.out.println("id = " + id);
        System.out.println(hqImm.getImmId()+"+++++"+hqImm.getImmName());
        return hqImm;
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public List<HqImm> queryAll(HqImm hqImm){
        hqImm.setIsva(1);
//        List<HqImm> list = hqImmService.queryAll(hqImm);
//        Map<String,Object> map = new HashMap<>();
//        map.put("data", list);
//        map.put("count", null);
//        map.put("msg","");
//        map.put("code",0);
        return hqImmService.queryAll(hqImm);
    }

    @PostMapping("queryImmName")
    public List<HqImm> queryImmName(){
        List<HqImm> list = hqImmService.queryImmName();
        return list;
    }

}